package xyx.ysfwpt.controller;

import io.swagger.annotations.*;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import xyx.ysfwpt.model.InputList1;
import xyx.ysfwpt.model.Order;
import xyx.ysfwpt.model.OrderDetail;
import xyx.ysfwpt.model.User;
import xyx.ysfwpt.service.OrderService;
import xyx.ysfwpt.service.UserService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ysfwpt")
@Api(tags = {"订单接口"},description = "")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    UserService userService;

    @ApiOperation(value = "订单管理",notes = "查询所有订单")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询成功")})
    @RequestMapping(value="/selectAllOrder",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject selectAllOrder(){
        JSONObject jsonObject = new JSONObject();
        List list = orderService.selectAllOrder();
        jsonObject.put("list",list);
        return jsonObject;
    }

    @ApiOperation(value = "订单管理",notes = "根据医院/科室/来源/状态/查询词/下单时间/类型查询当前用户名下的订单信息")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询成功")})
    @RequestMapping(value="/selectOrderByInputList",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject selectOrderByInputList(@ApiParam(value = "医生工号",required = true) @RequestParam(value = "user_id",required = true)int user_id,
                                             @ApiParam(value = "权限",required = false) @RequestParam(value = "user_type",required = false)String user_type,
                                             @ApiParam(value = "医院",required = false) @RequestParam(value = "hospital",required = false)String hospital,
                                             @ApiParam(value = "科室",required = false) @RequestParam(value = "user_department",required = false)String user_department,
                                             @ApiParam(value = "状态",required = false) @RequestParam(value = "state",required = false)Integer state,
                                             @ApiParam(value = "来源",required = false) @RequestParam(value = "source",required = false)String source,
                                             @ApiParam(value = "类型",required = false) @RequestParam(value = "type",required = false)Integer type,
                                             @ApiParam(value = "姓名/联系方式",required = false) @RequestParam(value = "searchword",required = false)String searchword,
                                             @ApiParam(value = "开始时间",required = false) @RequestParam(value = "timefrom",required = false)String timefrom,
                                             @ApiParam(value = "结束时间",required = false) @RequestParam(value = "timeto",required = false)String timeto){
        JSONObject jsonObject = new JSONObject();
        //根据当前登录用户的工号查询用户信息
        User user = userService.selectUserByJobNumber(user_id);
        String isDirector = "";
        String department = "";
        //如果该用户工号等于科室负责人工号，说明该用户就是科室负责人
        if(user.getJob_number().equals(user.getDirector())){
            isDirector = "YES";
            department = user.getDepartment();
        }
        else{
            isDirector = "NO";
        }
        List list = orderService.selectOrderByInputList(user_id,user_type,hospital,user_department,state,source,type,searchword,timefrom,timeto,isDirector,department);
        jsonObject.put("list",list);
        return jsonObject;
    }

    @ApiOperation(value = "订单管理",notes = "根据订单编号查询订单所含药品名称")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询成功")})
    @RequestMapping(value="/selectMedicineNameByOrderId",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject selectMedicineNameByOrderId(@ApiParam(value = "订单编号",required = true) @RequestParam(value = "order_id",required = true)String order_id){
        JSONObject jsonObject = new JSONObject();
        List list = orderService.selectMedicineByOrderId(order_id);
        //获取到的数据为list类型，将每个数据中的名称取出，重新拼接
        String medicineName = "";
        for (int i = 0; i < list.size(); i++) {
            OrderDetail obj=( OrderDetail)list.get(i);
            medicineName = medicineName + obj.getOrder_drug_name() + ";";
        }
        jsonObject.put("medicineName",medicineName);
        return jsonObject;
    }

    @ApiOperation(value = "订单管理",notes = "根据订单编号查询订单所含药品全部信息")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询成功")})
    @RequestMapping(value="/selectMedicineByOrderId",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject selectMedicineByOrderId(@ApiParam(value = "订单编号",required = true) @RequestParam(value = "order_id",required = true)String order_id){
        JSONObject jsonObject = new JSONObject();
        List list = orderService.selectMedicineByOrderId(order_id);
        //计算总价
        float total = 0;
        for (int i = 0; i < list.size(); i++) {
            OrderDetail obj=( OrderDetail)list.get(i);
            total = total + obj.getOrder_total_price()*100;
        }
        jsonObject.put("list",list);
        jsonObject.put("total",total/100);
        return jsonObject;
    }

    @ApiOperation(value = "订单管理",notes = "根据订单编号查询订单所有信息")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询成功")})
    @RequestMapping(value="/selectOrderDetailByOrderId",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject selectOrderDetailByOrderId(@ApiParam(value = "订单编号",required = true) @RequestParam(value = "order_id",required = true)String order_id){
        JSONObject jsonObject = new JSONObject();
        Order order = orderService.selectOrderDetailByOrderId(order_id);
        jsonObject.put("order",order);
        return jsonObject;
    }

    @ApiOperation(value = "订单管理",notes = "根据订单编号查询订单取药编码，若编码正确，将订单状态修改")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询成功")})
    @RequestMapping(value="/selectMedicineNOByOrderId",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject selectMedicineNOByOrderId(@ApiParam(value = "订单编号",required = true) @RequestParam(value = "order_id",required = true)String order_id,
                                                @ApiParam(value = "取药编码",required = true) @RequestParam(value = "medicineNO",required = true)String medicineNO){
        JSONObject jsonObject = new JSONObject();
        Order order = orderService.selectOrderDetailByOrderId(order_id);
        String number = order.getMedicineNO();
        //取药编码正确
        if(number.equals(medicineNO)){
            jsonObject.put("result",1);
            //将订单状态从“待取药”修改为“已取药”
            orderService.updateOrderState(order_id,4,"","");
        }
        //取药编码错误
        else{
            jsonObject.put("result",2);
        }
        return jsonObject;
    }

    @ApiOperation(value = "修改订单状态",notes = "根据订单编号修改订单状态，并写入修改订单状态的理由")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "修改成功")})
    @RequestMapping(value="/updateOrderState",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject updateOrderState(@ApiParam(value = "订单编号",required = true) @RequestParam(value = "order_id",required = true)String order_id,
                                       @ApiParam(value = "状态",required = true) @RequestParam(value = "state",required = true)Integer state,
                                       @ApiParam(value = "原因名称",required = false) @RequestParam(value = "reason_name",required = false)String reason_name,
                                       @ApiParam(value = "原因内容",required = false) @RequestParam(value = "reason",required = false)String reason){
        JSONObject jsonObject = new JSONObject();
        orderService.updateOrderState(order_id,state,reason_name,reason);
        return jsonObject;
    }

    @ApiOperation(value = "修改订单快递信息",notes = "根据订单编号写入快递公司名称，快递费用，快递单号")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "修改成功")})
    @RequestMapping(value="/updateOrderExpress",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject updateOrderExpress(@ApiParam(value = "订单编号",required = true) @RequestParam(value = "order_id",required = true)String order_id,
                                       @ApiParam(value = "快递公司名称",required = true) @RequestParam(value = "express_company_name",required = true)String express_company_name,
                                       @ApiParam(value = "快递费用",required = true) @RequestParam(value = "express_price",required = true)float express_price,
                                       @ApiParam(value = "快递单号",required = true) @RequestParam(value = "express_id",required = true)String express_id){
        JSONObject jsonObject = new JSONObject();
        orderService.updateOrderExpress(order_id,express_company_name,express_price,express_id);
        return jsonObject;
    }

    @ApiOperation(value = "订单统计",notes = "查询订单总数、已完成订单数量、订单总金额、药品总金额、快递总金额")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询成功")})
    @RequestMapping(value="/selectOrderAmountAndMoney",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject selectOrderAmountAndMoney(@ApiParam(value = "类别",required = true) @RequestParam(value = "type",required = true)Integer type,
                                                @ApiParam(value = "开始时间",required = true) @RequestParam(value = "timefrom",required = true)String timefrom,
                                                @ApiParam(value = "结束时间",required = true) @RequestParam(value = "timeto",required = true)String timeto){
        JSONObject jsonObject = new JSONObject();
        //对时间进行格式补齐
        //解决无法查询当天数据的问题
        String from = timefrom + " 00:00:00";
        String to = timeto + " 23:59:59";
        Map map = new HashMap();
        //type == 0时查询全部订单，分别查询“到店自取”和“在线配送”订单信息，在前端页面相加，计算全部
        if(type == 0){
            Map map1 = orderService.selectOrderAmountAndMoney1(from,to);
            Map map2 = orderService.selectOrderAmountAndMoney2(from,to);
            jsonObject.put("map1",map1);
            jsonObject.put("map2",map2);
        }
        else if(type == 1){
            map = orderService.selectOrderAmountAndMoney1(from,to);
            jsonObject.put("map",map);
        }
        else if(type == 2){
            map = orderService.selectOrderAmountAndMoney2(from,to);
            jsonObject.put("map",map);
        }
        return jsonObject;
    }

    @ApiOperation(value = "订单统计",notes = "根据年份、订单类别（在线、自取）查询全年各类订单数量")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询成功")})
    @RequestMapping(value="/selectOrderAmountByYear",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject selectOrderAmountByYear(@ApiParam(value = "年份",required = true) @RequestParam(value = "year",required = true)String year,
                                              @ApiParam(value = "类别",required = true) @RequestParam(value = "type",required = true)String type){
        JSONObject jsonObject = new JSONObject();
        String yearfrom = year + "-01-01";
        String yearto = year + "-12-31";
        List list = orderService.selectOrderAmountByYear(yearfrom,yearto,type);
        jsonObject.put("list",list);
        return jsonObject;
    }

    @ApiOperation(value = "订单统计",notes = "根据订单类别（在线、自取）查询每天订单总数、已完成订单数量、订单总金额、药品总金额、快递总金额")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "查询成功")})
    @RequestMapping(value="/selectOrderByDay",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject selectOrderByDay(@ApiParam(value = "类别",required = true) @RequestParam(value = "type",required = true)Integer type,
                                       @ApiParam(value = "开始时间",required = true) @RequestParam(value = "timefrom",required = true)String timefrom,
                                       @ApiParam(value = "结束时间",required = true) @RequestParam(value = "timeto",required = true)String timeto){
        JSONObject jsonObject = new JSONObject();
        //对时间进行格式补齐
        //解决无法查询当天数据的问题
        String from = timefrom + " 00:00:00";
        String to = timeto + " 23:59:59";
        List list = new ArrayList();
        if(type == 0){
            list = orderService.selectOrderByDay0(from,to);
        }
        else if(type == 1){
            list = orderService.selectOrderByDay1(from,to);
        }
        else if(type == 2){
            list = orderService.selectOrderByDay2(from,to);
        }
        jsonObject.put("list",list);
        return jsonObject;
    }

}